home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / QuickTime / JPEG Sample / Source / windows.c < prev   
Encoding:
C/C++ Source or Header  |  1997-03-05  |  11.6 KB  |  494 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #        Windows.c
  4. #
  5. #        This segment handles the window creation, close, updates,
  6. #
  7. #        Author(s):     Michael Marinkovich & Guillermo Ortiz
  8. #                    marink@apple.com
  9. #
  10. #        Modification History: 
  11. #
  12. #            4/3/96        MWM     Initial coding                     
  13. #
  14. #        Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
  15. #
  16. #
  17. #        You may incorporate this sample code into your applications without
  18. #        restriction, though the sample code has been provided "AS IS" and the
  19. #        responsibility for its operation is 100% yours.  However, what you are
  20. #        not permitted to do is to redistribute the source as "DSC Sample Code"
  21. #        after having made changes. If you're going to re-distribute the source,
  22. #        we require that you make it clear in the source that the code was
  23. #        descended from Apple Sample Code, but that you've made changes.
  24. #
  25. *************************************************************************************/
  26.  
  27. #include <TextUtils.h>
  28. #include <Windows.h>
  29.  
  30. #include "App.h"
  31. #include "Proto.h"
  32.  
  33.  
  34. //----------------------------------------------------------------------
  35. //
  36. //    Globals - 
  37. //
  38. //----------------------------------------------------------------------
  39.  
  40. extern Boolean        gHasAbout;        // have an about box?
  41. extern short        gWindCount;
  42.  
  43.  
  44. //----------------------------------------------------------------------
  45. //
  46. //    CreateWindow - create a window from the info passed in. Will try to 
  47. //                   load from resource if resID is supplied.
  48. //
  49. //----------------------------------------------------------------------
  50.  
  51. WindowPtr CreateWindow(short resID, void *wStorage, Rect *bounds,  Str255 title,
  52.                        Boolean visible, short procID , short kind,
  53.                        WindowRef behind, Boolean goAwayFlag, long refCon)
  54. {
  55.     OSErr            err = nil;
  56.     WindowRef        newWindow = nil;
  57.     
  58.     
  59.     if (resID != nil)         // if res id isn't nil then load from disk
  60.         newWindow = GetNewWindow(resID, wStorage, behind);
  61.     else                    // otherwise make a new windowRecord
  62.          newWindow = NewWindow(wStorage, bounds, title, visible, 
  63.                                procID, behind, goAwayFlag, refCon);
  64.                                
  65.     if (newWindow != nil) 
  66.     {
  67.         NewWindowTitle(newWindow, title);
  68.         err = InitWindowProcs(newWindow, kind);
  69.         
  70.         if (err == noErr) 
  71.         {
  72.             SetPort(newWindow);
  73.             ShowWindow(newWindow);
  74.         }
  75.                 
  76.         // initialization of Document Record failed
  77.         // so kill the return window    
  78.         else 
  79.         {
  80.             newWindow = nil;
  81.             HandleError(err, false);
  82.         }        
  83.     }
  84.     
  85.     return newWindow;
  86.  
  87. }
  88.     
  89.  
  90. //----------------------------------------------------------------------
  91. //
  92. //    RemoveWindow - applications Doc window disposal routine.
  93. //                 
  94. //
  95. //----------------------------------------------------------------------
  96.  
  97. OSErr RemoveWindow( WindowRef window )
  98. {
  99.     OSErr            err = nil;
  100.     short            kind;
  101.     DocHnd            doc;
  102.     
  103.     
  104.     kind = GetWindKind( window );
  105.     if ( kind < kDocKind || kind > kAboutKind )
  106.         return -1; // not our window
  107.     
  108.     doc = (DocHnd)GetWRefCon( window );
  109.     if ( doc != nil ) {
  110.         switch( kind ) {
  111.             case kDocKind:
  112.                 DisposeGWorld((**doc).world);
  113.                 DisposeHandle( (Handle)doc );
  114.                 DisposeWindow( window );
  115.                 err = noErr;
  116.                 break;
  117.             
  118.             case kAboutKind:
  119.                 DisposeHandle( (Handle)doc );
  120.                 DisposeWindow( window );
  121.                 err = noErr;
  122.                 gHasAbout = false;    // allow new about box
  123.                 break;
  124.             
  125.             default:
  126.                 break;
  127.                     
  128.  
  129.         }    
  130.     }
  131.     
  132.     return err;
  133. }        
  134.  
  135.  
  136. //----------------------------------------------------------------------
  137. //
  138. //    NewWindowTitle - if supplied title is nil then title is set to 
  139. //                      global "gWindCount".
  140. //
  141. //----------------------------------------------------------------------
  142.  
  143. void NewWindowTitle(WindowRef window, Str255 str)
  144. {
  145.     Str255        catStr = "\pUntitled ";
  146.     Str255        newStr;
  147.     
  148.     
  149.     if (str == nil || StrLength(str) == 0) 
  150.     {
  151.         pstrcpy(newStr, catStr);
  152.         NumToString(gWindCount, catStr);
  153.         pstrcat(newStr, catStr);
  154.         gWindCount++;
  155.         
  156.         SetWTitle(window,newStr);
  157.     }
  158.     else
  159.         SetWTitle(window, str);
  160.  
  161. }
  162.  
  163.  
  164. //----------------------------------------------------------------------
  165. //
  166. //    InitWindowProcs - init a window with proper callback event. fills 
  167. //                       out custom procs for different windowkinds.
  168. //
  169. //----------------------------------------------------------------------
  170.  
  171. OSErr InitWindowProcs(WindowRef window, short windKind)
  172. {
  173.     OSErr            err = nil;
  174.     DocHnd            doc;
  175.     
  176.     doc = (DocHnd)NewHandle(sizeof(DocRec));
  177.     if (doc != nil) {
  178.         SetWRefCon(window, (long)doc);
  179.  
  180.         switch(windKind) {
  181.             case kDocKind:
  182.                 (**doc).idleProc        = DoIdle;
  183.                 (**doc).mMenuProc        = HandleMenuChoice;
  184.                 (**doc).inContentProc    = HandleContentClick;
  185.                 (**doc).inGoAwayProc    = nil;
  186.                 (**doc).inZoomProc        = HandleZoomClick;
  187.                 (**doc).inGrowProc        = HandleGrow;
  188.                 (**doc).keyProc            = nil;
  189.                 (**doc).activateProc    = DoActivate;
  190.                 (**doc).updateProc        = DrawWindow;    
  191.                 (**doc).hScroll         = nil;
  192.                 (**doc).vScroll            = nil;
  193.                 (**doc).world            = nil;
  194.                 (**doc).pict            = nil;
  195.                 (**doc).printer            = nil;
  196.                 (**doc).dirty            = false;
  197.                 
  198.                 InstallScrollBars(window, doc);
  199.  
  200.                 break;
  201.                 
  202.             case kDialogKind:
  203.                 (**doc).idleProc        = DoIdle;
  204.                 (**doc).mMenuProc        = HandleMenuChoice;
  205.                 (**doc).inContentProc    = nil;
  206.                 (**doc).inGoAwayProc    = nil;
  207.                 (**doc).inZoomProc        = nil;
  208.                 (**doc).inGrowProc        = nil;
  209.                 (**doc).keyProc            = nil;
  210.                 (**doc).activateProc    = nil;
  211.                 (**doc).updateProc        = DrawAboutWindow;    
  212.                 (**doc).hScroll         = nil;
  213.                 (**doc).vScroll            = nil;
  214.                 (**doc).world            = nil;
  215.                 (**doc).pict            = GetPicture(rAboutPictID);
  216.                 (**doc).printer            = nil;
  217.                 (**doc).dirty            = false;
  218.                 break;
  219.  
  220.             case kAboutKind:
  221.                 (**doc).idleProc        = DoIdle;
  222.                 (**doc).mMenuProc        = HandleMenuChoice;
  223.                 (**doc).inContentProc    = nil;
  224.                 (**doc).inGoAwayProc    = nil;
  225.                 (**doc).inZoomProc        = nil;
  226.                 (**doc).inGrowProc        = nil;
  227.                 (**doc).keyProc            = nil;
  228.                 (**doc).activateProc    = nil;
  229.                 (**doc).updateProc        = DrawAboutWindow;    
  230.                 (**doc).hScroll         = nil;
  231.                 (**doc).vScroll            = nil;
  232.                 (**doc).world            = nil;
  233.                 (**doc).pict            = GetPicture(rAboutPictID);
  234.                 (**doc).printer            = nil;
  235.                 (**doc).dirty            = false;
  236.                 
  237.                 break;
  238.                 
  239.             default:
  240.                 err = 25;
  241.                 break;    
  242.         }
  243.         ((WindowPeek)window)->windowKind = windKind;
  244.  
  245.     }            
  246.     return err;
  247.  
  248. }
  249.  
  250.  
  251. //----------------------------------------------------------------------
  252. //
  253. //    PictToWorld - create a GWorld from a Pict. Size is determined by the
  254. //                  bounds of the pict. If the pict is nil then a default
  255. //                  bounds is used, in case of an empty window.
  256. //----------------------------------------------------------------------
  257.     
  258. GWorldPtr PictToWorld(PicHandle pict, OSErr *rtnErr)
  259. {
  260.     OSErr            err;
  261.     GWorldPtr        oldWorld;
  262.     GWorldPtr        theWorld = nil;
  263.     GDHandle        oldGD;
  264.     PixMapHandle    thePix;
  265.     Rect            bounds;
  266.     
  267.     GetGWorld(&oldWorld, &oldGD);
  268.     
  269.     if (pict != nil)
  270.         bounds = (**pict).picFrame;
  271.     else
  272.         SetRect(&bounds, 0, 0, 200, 200);    
  273.     
  274.     err = NewGWorld(&theWorld, 8,&bounds, nil, nil, 0L);
  275.     if (err == noErr && theWorld != nil) {
  276.         thePix = GetGWorldPixMap(theWorld);
  277.         if (LockPixels(thePix)) {
  278.             SetGWorld(theWorld, nil);
  279.             EraseRect(&bounds);
  280.             if (pict != nil)
  281.                 DrawPicture(pict, &bounds);
  282.             
  283.             UnlockPixels(thePix);
  284.         }
  285.         SetGWorld(oldWorld, oldGD);
  286.     }
  287.     
  288.     *rtnErr = err;
  289.     
  290.     return theWorld;
  291. }
  292.  
  293.         
  294. //----------------------------------------------------------------------
  295. //
  296. //    DrawWindow - custom proc that is called to update window contents.
  297. //                 
  298. //
  299. //----------------------------------------------------------------------
  300.  
  301. void DrawWindow(WindowRef window, void *refCon)
  302. {
  303.     #pragma unused (refCon)
  304.  
  305.     DocHnd            doc;
  306.     GWorldPtr        theWorld;
  307.     PixMapHandle    thePix;
  308.     Rect            cRect;
  309.     Rect            bounds;
  310.     
  311.     doc = (DocHnd)GetWRefCon(window);    
  312.     if (doc != nil) {
  313.         SetPort(window);
  314.         GetContRect(window, &cRect);
  315.         ClipRect(&cRect);
  316.         
  317.         theWorld = (**doc).world;
  318.  
  319.         if (theWorld != nil ) {
  320.             bounds = theWorld->portRect;
  321.             OffsetRect(&bounds, -GetCtlValue((**doc).hScroll), 
  322.                        -GetCtlValue((**doc).vScroll));
  323.             thePix = GetGWorldPixMap(theWorld);
  324.             if (LockPixels(thePix)) {
  325.                 CopyBits((BitMap *) *thePix, &window->portBits,
  326.                          &theWorld->portRect, &bounds, srcCopy, nil);
  327.                 
  328.                 UnlockPixels(thePix);
  329.             }             
  330.                         
  331.         }
  332.         else
  333.             EraseRect(&cRect);    
  334.  
  335.         ClipRect(&window->portRect);
  336.         DrawGrowIcon(window);
  337.         UpdateControls(window, window->visRgn);
  338.     }
  339.  
  340. }
  341.  
  342.  
  343. //----------------------------------------------------------------------
  344. //
  345. //    DrawAboutWindow - custom proc that is called to update about window.
  346. //                 
  347. //
  348. //----------------------------------------------------------------------
  349.  
  350. void DrawAboutWindow( WindowRef window, void *refCon )
  351. {    
  352.     #pragma unused (refCon)
  353.  
  354.     DocHnd        doc;
  355.     
  356.     doc = (DocHnd)GetWRefCon(window);    
  357.     if ( (**doc).pict != nil ) 
  358.         DrawPicture( (**doc).pict, &window->portRect );
  359.  
  360. }
  361.  
  362.  
  363. //----------------------------------------------------------------------
  364. //
  365. //    DoResizeWindow - custom proc that is called to update window.
  366. //                 
  367. //
  368. //----------------------------------------------------------------------
  369.  
  370. void DoResizeWindow (WindowRef window) 
  371. {
  372.     DocHnd            doc;
  373.     ControlHandle    hCtl, vCtl;
  374.     RgnHandle        tempRgn;
  375.     RgnHandle        oldCtlRgn;
  376.     short            max, oldCntlVal;
  377.     short            pV, wV;
  378.     short             pH, wH;
  379.     Rect            hSRect;
  380.     Rect            newDocRect;
  381.     Rect            pictRect;
  382.     Rect            paneRect;
  383.  
  384.     doc = (DocHnd)GetWRefCon(window);
  385.     if (doc != nil) {
  386.         pictRect = (**doc).world->portRect;
  387.         newDocRect = (**window->visRgn).rgnBBox;
  388.  
  389.         hCtl = (**doc).hScroll;
  390.         vCtl = (**doc).vScroll;
  391.         
  392.         hSRect = (**hCtl).contrlRect;
  393.         hSRect.right += kScrollWidth;
  394.         ClipRect(&window->portRect);
  395.         RectRgn(oldCtlRgn = NewRgn(), &hSRect);
  396.         RectRgn(tempRgn = NewRgn(), &(**vCtl).contrlRect);
  397.         UnionRgn(oldCtlRgn, tempRgn, oldCtlRgn);
  398.         EraseRgn(oldCtlRgn);
  399.         
  400.         (**hCtl).contrlVis = 0;
  401.         (**vCtl).contrlVis = 0;
  402.  
  403.         paneRect = newDocRect;
  404.         paneRect.bottom -= kScrollWidth;
  405.         paneRect.right -= kScrollWidth;
  406.         
  407.         MoveControl(hCtl, paneRect.left - 1, paneRect.bottom);    
  408.         MoveControl(vCtl, paneRect.right, paneRect.top - 1);
  409.             
  410.         SizeControl(hCtl, 2+paneRect.right - paneRect.left, kScrollWidth + 1);    
  411.         SizeControl(vCtl, kScrollWidth + 1, 2+paneRect.bottom - paneRect.top);
  412.         
  413.         (**hCtl).contrlVis = 255;
  414.         (**vCtl).contrlVis = 255;
  415.         
  416.         pV = pictRect.bottom - pictRect.top;
  417.         wV = paneRect.bottom - paneRect.top;
  418.         if (wV >= pV)
  419.             max = 0;
  420.         else
  421.             max = ((MAX (0, pV - wV)) / kScrollDelta) + 1;
  422.             
  423.         oldCntlVal = GetControlValue(vCtl);
  424.         SetControlMinimum(vCtl, 0);
  425.         SetControlMaximum(vCtl, max);
  426.         SetControlValue(vCtl, oldCntlVal );
  427.         
  428.         HiliteControl(vCtl, (max == 0) ? (255):(0));    
  429.  
  430.         pH = pictRect.right - pictRect.left;
  431.         wH = paneRect.right - paneRect.left;
  432.         if (wH >= pH)
  433.             max = 0 ;
  434.         else
  435.             max = ((MAX (0, pH - wH )) / kScrollDelta) +1;
  436.         oldCntlVal = GetControlValue(hCtl);
  437.         SetControlMinimum(hCtl, 0);
  438.         SetControlMaximum(hCtl, max) ;
  439.         SetControlValue(hCtl, oldCntlVal);
  440.         
  441.         HiliteControl(hCtl, (max == 0) ? (255):(0));    
  442.                 
  443.         DisposeRgn(oldCtlRgn);
  444.         DisposeRgn(tempRgn);
  445.         AdjustScrollValues(window);
  446.         InvalRect(&window->portRect);
  447.  
  448.     }
  449.  
  450. }
  451.  
  452.  
  453. //----------------------------------------------------------------------
  454. //
  455. //    GetWindKind - returns the windowkind.
  456. //                 
  457. //
  458. //----------------------------------------------------------------------
  459.  
  460. short GetWindKind(WindowRef window)
  461. {
  462.  
  463.     return ((WindowPeek)window)->windowKind;
  464.  
  465. }
  466.  
  467.  
  468. //----------------------------------------------------------------------
  469. //
  470. //    GetIsAppWindow - is the window a 'userKind'.
  471. //                 
  472. //
  473. //----------------------------------------------------------------------
  474.  
  475. Boolean GetIsAppWindow(WindowRef window)
  476. {
  477.     return (GetWindKind(window) == kDocKind);
  478.  
  479. }
  480.  
  481.  
  482. //----------------------------------------------------------------------
  483. //
  484. //    GetIsAboutWindow - is the window an about box.
  485. //                 
  486. //
  487. //----------------------------------------------------------------------
  488.  
  489. Boolean GetIsAboutWindow(WindowRef window)
  490. {
  491.     return (GetWindKind(window) == kAboutKind);
  492.     
  493. }
  494.